package com.example.ymm.modules1.service.dd.iot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.util.HttpClientUtil;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 亮灯系统请求类
 * @author zeymo
 */
@Slf4j
@Service
public class IotHttpUtil extends HttpClientUtil {


    @Value("${iot.domain}")
    private String domain;


    /**
     * get 获取详情
     * @param url
     * @param param
     * @return
     */
    public JSONObject getDetail(String url, JSONObject param){
        String s = httpGet(domain+url, param, null);
        JSONObject jsonNode=null;
        JSONObject data=null;
        try {
            if(UtilValidate.isNotEmpty(s)){
                jsonNode = JSONObject.parseObject(s);
                Integer code = jsonNode.getInteger("code");
                String msg = jsonNode.getString("msg");
                if(code==200){
                    data=jsonNode.getJSONObject("data");
                }else{
                    throw new YmException("code="+code+",msg="+msg);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return data;
    }

    /**
     * get 获取列表
     * @param url
     * @param param
     * @return
     */
    public TableList<JSONObject> getList(String url, JSONObject param){
        String s = httpGet(domain+url, param, null);
        JSONObject jsonNode=null;
        TableList<JSONObject> tableList=new TableList<>();
        try {
            if(UtilValidate.isNotEmpty(s)){
                jsonNode = JSONObject.parseObject(s);
                Integer code = jsonNode.getInteger("code");
                String msg = jsonNode.getString("msg");
                Long total = jsonNode.getLong("total");
                if(code==200){
                    JSONArray jsonArray = jsonNode.getJSONArray("data");
                    List<JSONObject> list=jsonArray.toJavaList(JSONObject.class);
                    tableList.setTotalResult(total);
                    tableList.setList(list);
                }else{
                    throw new YmException("code="+code+",msg="+msg);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return tableList;
    }

    public JSONObject httpPost(String url, JSONObject param){
        String s = httpPost(domain+url, param, null);
        JSONObject jsonNode=null;
        JSONObject data=null;
        try {
            if(UtilValidate.isNotEmpty(s)){
                jsonNode = JSONObject.parseObject(s);
                Integer code = jsonNode.getInteger("code");
                String msg = jsonNode.getString("fieldErrors");
                if(code==200){
                    data=jsonNode.getJSONObject("data");
                }else{
                    throw new YmException(msg);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonNode;
    }

    public JSONObject httpPostStr(String url, String arrStr){
        String s = httpPostStr(domain+url, arrStr, null);
        JSONObject jsonNode=null;
        JSONObject data=null;
        try {
            if(UtilValidate.isNotEmpty(s)){
                jsonNode = JSONObject.parseObject(s);
                Integer code = jsonNode.getInteger("code");
                String msg = jsonNode.getString("fieldErrors");
                if(code==200){
                    data=jsonNode.getJSONObject("data");
                }else{
                    throw new YmException(msg);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonNode;
    }


    public JSONObject httpPut(String url, JSONObject param){
        String s = httpPut(domain+url, param, null);
        JSONObject jsonNode=null;
        JSONObject data=null;
        try {
            if(UtilValidate.isNotEmpty(s)){
                jsonNode = JSONObject.parseObject(s);
                Integer code = jsonNode.getInteger("code");
                String msg = jsonNode.getString("msg");
                if(code==200){
                    data=jsonNode.getJSONObject("data");
                }else{
                    throw new YmException("code="+code+",msg="+msg);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return data;
    }

    public JSONObject httpDelete(String url, JSONObject param){
        String s = httpDelete(domain+url, param, null);
        JSONObject jsonNode=null;
        JSONObject data=null;
        try {
            if(UtilValidate.isNotEmpty(s)){
                jsonNode = JSONObject.parseObject(s);
                Integer code = jsonNode.getInteger("code");
                String msg = jsonNode.getString("msg");
                if(code==200){
                    data=jsonNode.getJSONObject("data");
                }else{
                    throw new YmException("code="+code+",msg="+msg);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return data;
    }


}
